﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMFC
{
    public partial class frmTMFC001 : Form
    {
        string strUser;

        string longSartTitle = "异常状况描述:";
        string longGetTitle = "生产现状&原因分析:";
        string longDoTitle = "处理办法:";
        string longOKTitle = "处理结果验证:";

        public frmTMFC001()
        {
            InitializeComponent();
        }

        //新建时使用
        public frmTMFC001(string user)
        {
            InitializeComponent();
            strUser = user;
            txtStartUsr.Text = user;
            txtStartDept.Text = getMesDeptAndName(user);
            txtProgress.Text = "新建";

            cmbStatus.Text = "未结案";
            cmbStatus.Enabled = false;
            

            getNgrID();  //取单号
            getNumPS(); //取工单            
            getItemEQ(); //取机台别
            getShiftProd();//取班别
            GetcmbNGItem();
        }

        //修改时使用
        public frmTMFC001(string user, string id_ngr)
        {
            InitializeComponent();
            strUser = user;
            NGR_ID.Text = id_ngr;           


            //根据ID查询出本单据所有信息
            getAllInfoByID(id_ngr);
            txtProgress.Text = "修改";
            if (!cmbStatus.Text.Equals("未结案"))
            {
                btnSubmit.Enabled = false;
                groupBox1.Enabled = false;
            }


        }

        private void frmTMFC001_Load(object sender, EventArgs e)
        {
            //this.dtpStartDate.Format = DateTimePickerFormat.Custom;
            //this.dtpStartDate.CustomFormat = " ";

            //this.dtpGetDate.Format = DateTimePickerFormat.Custom;
            //this.dtpGetDate.CustomFormat = " ";

            //this.dtpDoDate.Format = DateTimePickerFormat.Custom;
            //this.dtpDoDate.CustomFormat = " ";

            //this.dtpOKDate.Format = DateTimePickerFormat.Custom;
            //this.dtpOKDate.CustomFormat = " ";

           
        }

        #region getNgrID() 获取单号
        private void getNgrID()
        {
            string dept = getMesDept(strUser);
            string strSeq = Utile.clsUtile.GetSeqWithoutSpval("FD_TMFCNGR");
            NGR_ID.Text = dept + "-" + strSeq;
        }
        #endregion

        #region getNumPS() 工单查询
        private void getNumPS()
        {
            cmbNumPs.Items.Clear();
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();
            string strSQL = "SELECT distinct NUM_PS FROM FDCLDS WHERE STS_PS = 'PROD' " +
                " and substr(id_route,4,3) <> 'OQC'" +//排除OQC途程
                " ORDER BY NUM_PS";
            string strResult = DBLink.executeQuery(strSQL, dtTable);
            if (!strResult.Equals(""))
            {
                MessageBox.Show("查询生产中工单时发生异常!");

            }
            else
                for (int i = 0; i <= dtTable.Rows.Count - 1; i++)
                    cmbNumPs.Items.Add(dtTable.Rows[i][0].ToString());
            dtTable.Dispose();
            dtTable = null;
        }
        #endregion

        #region getItemEQ() 机台
        private void getItemEQ()
        {
            cmbNamEq.Items.Clear();
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();
            string strSQL = "select nam_eq from fdequip where is_cross='Y' order by nam_eq";
            string strResult = DBLink.executeQuery(strSQL, dtTable);
            if (!strResult.Equals(""))
            {
                MessageBox.Show("查询机台信息时发生异常!");

            }
            else
                for (int i = 0; i <= dtTable.Rows.Count - 1; i++)
                    cmbNamEq.Items.Add(dtTable.Rows[i][0].ToString());
            dtTable.Dispose();
            dtTable = null;
        }
        #endregion

        #region getShiftProd() 取班别
        private void getShiftProd()
        {
            string prod_shift = "";
            string date2 = dtpTimFrom.Value.TimeOfDay.ToString().Substring(0, 8);
            //string date1 = dtpTimFrom.Value.Hour.ToString("HH") + ":" + dtpTimFrom.Value.Minute.ToString("mm") + ":" + dtpTimFrom.Value.Second.ToString("ss");
            //date1 = date1.Substring(11);
            if (System.DateTime.Parse(date2) >= System.DateTime.Parse("08:00:00")
                && System.DateTime.Parse(date2) < System.DateTime.Parse("20:00:00"))
                prod_shift = "日班";
            else
                prod_shift = "夜班";

            cmbSftProd.Text = prod_shift;
        }
        #endregion


        /// <summary>
        /// 根据ID查询出本单据所有信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getAllInfoByID(string strID)
        {
            string sql = "select * from fd_tmfcngr where id_ngr ='"+strID+"'";
            DataTable dt = new DataTable();
            DB.DB dblink = new DB.DB();
            string strResult = dblink.executeQuery(sql, dt);
            if (dt.Rows.Count != 1)
            {
                lblTips.Text = "此编号不存在,请重新输入!";
                return;
            }

            //获取各项值
            #region 填充界面数据    

            NGR_ID.Text = dt.Rows[0]["ID_NGR"].ToString();
            txtProgress.Text = dt.Rows[0]["PROGRESS"].ToString();

            cmbStatus.Text= dt.Rows[0]["STATUSCODE"].ToString();
            string FAST_TYPE = dt.Rows[0]["FAST_TYPE"].ToString();//急件类型
            setControlRB(gbFastType, FAST_TYPE);
            cmbNGItem.Text = dt.Rows[0]["NGR_TYPE"].ToString();
            cmbNumPs.Text = dt.Rows[0]["NUM_PS"].ToString();
            cmbLot.Text = dt.Rows[0]["LOT"].ToString();
            cmbSftProd.Text = dt.Rows[0]["SHIFT_PROD"].ToString();
            cmbNamEq.Text = dt.Rows[0]["NAM_EQ"].ToString();
            txtStartUsr.Text = dt.Rows[0]["START_USR"].ToString();
            txtStartDept.Text = dt.Rows[0]["START_DEPT"].ToString();
            dtpStartDate.Text = dt.Rows[0]["START_DATE"].ToString();
            longWt.Text = dt.Rows[0]["START_NOTE"].ToString();

            txttnUsr.Text = dt.Rows[0]["TN_USR"].ToString();
            string TN_DEPT = dt.Rows[0]["TN_DEPT"].ToString();//预计的接收部门            
            setControlRB(groupBox1, TN_DEPT);

            //接收            
            txtGetUsr.Text = dt.Rows[0]["GET_USR"].ToString();
            txtGetDept.Text = dt.Rows[0]["GET_DEPT"].ToString();
            dtpGetDate.Text = dt.Rows[0]["GET_DATE"].ToString();
            longAny.Text = dt.Rows[0]["ANY_NOTE"].ToString();
            //处理
            txtDoUsr.Text = dt.Rows[0]["DO_USR"].ToString();
            txtDoDept.Text = dt.Rows[0]["DO_DEPT"].ToString();
            dtpDoDate.Text = dt.Rows[0]["DO_DATE"].ToString();
            longDo.Text = dt.Rows[0]["DO_NOTE"].ToString();
            cmbDoRst.Text = dt.Rows[0]["DO_RESULT"].ToString();            
            //验收
            txtOKUsr.Text = dt.Rows[0]["OK_USR"].ToString();
            txtOKDept.Text = dt.Rows[0]["OK_DEPT"].ToString();
            dtpOKDate.Text = dt.Rows[0]["OK_DATE"].ToString();
            longOK.Text = dt.Rows[0]["OK_NOTE"].ToString();

            #endregion

        }



        private void groupBox1_Paint(object sender, PaintEventArgs e)
        {
            int pTop = 10;
            int pButtom = 622;
            int pLeft = 2;
            int pRight = 735;


            e.Graphics.DrawLine(Pens.Black, pLeft, pTop, pLeft, pButtom);//左边框
            e.Graphics.DrawLine(Pens.Black, pLeft - 1, pTop, pLeft - 1, pButtom);//左边框
            e.Graphics.DrawLine(Pens.Black, pRight, pTop, pRight, pButtom);//右边框
            e.Graphics.DrawLine(Pens.Black, pRight + 1, pTop, pRight + 1, pButtom);//右边框
            e.Graphics.DrawLine(Pens.Black, pLeft, pTop, pRight, pTop);//上边框
            e.Graphics.DrawLine(Pens.Black, pLeft, pTop - 1, pRight, pTop - 1);//上边框
            e.Graphics.DrawLine(Pens.Black, pLeft, pButtom, pRight, pButtom);//下边框
            e.Graphics.DrawLine(Pens.Black, pLeft, pButtom + 1, pRight, pButtom + 1);//下边框

            e.Graphics.DrawLine(Pens.Black, pLeft, 150, pRight, 150);//第一个横线
            e.Graphics.DrawLine(Pens.Black, pLeft, 238, pRight, 238);//第二个横线
            e.Graphics.DrawLine(Pens.Black, pLeft, 316, pRight, 316);//第三个横线
            e.Graphics.DrawLine(Pens.Black, pLeft, 400, pRight, 400);//第四个横线
            e.Graphics.DrawLine(Pens.Black, pLeft, 538, pRight, 538);//第五个横线
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //使用默认打印机
            string printName = new PrintDialog().PrinterSettings.DefaultPageSettings.PrinterSettings.PrinterName;
            //跳出打印对话框，提供打印参数可视化设置，如选择哪个打印机打印此文档等  
            PrintDialog pd = new PrintDialog();
            pd.Document = printDocument1;
            pd.Document.DocumentName = NGR_ID.Text.Trim();//文件编号设为打印出的文件名
            //if (this.printDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    this.printDocument1.Print();
            //}
            if (DialogResult.OK == pd.ShowDialog()) //如果确认，将会覆盖所有的打印参数设置  
            {
                //页面设置对话框（可以不使用，其实PrintDialog对话框已提供页面设置）  
                PageSetupDialog psd = new PageSetupDialog();
                psd.Document = printDocument1;
                if (DialogResult.OK == psd.ShowDialog())
                {
                    //打印预览  
                    PrintPreviewDialog ppd = new PrintPreviewDialog();
                    ppd.Document = printDocument1;
                    if (DialogResult.OK == ppd.ShowDialog())
                        printDocument1.Print(); //打印  
                }
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            ////打印内容 为 整个Form
            //Image myFormImage;
            //myFormImage = new Bitmap(this.Width, this.Height);
            //Graphics g = Graphics.FromImage(myFormImage);
            //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
            //e.Graphics.DrawImage(myFormImage, 0, 0);

            //打印内容 为 局部的 this.groupBox1
            Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
            groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);

            //打印内容 为 自定义文本内容 
            //Font font = new Font("宋体", 12);
            //Brush bru = Brushes.Blue;
            //for (int i = 1; i <= 5; i++)
            //{
            //    e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
            //}
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            GC.Collect();
            this.Close();
        }

        //获取控件内单选框
        private RadioButton getControlRB(Control ctl)
        {
            //RadioButton rb = new RadioButton();
            for (int i = 0; i < ctl.Controls.Count; i++)
            {
                if (ctl.Controls[i].GetType().FullName == "System.Windows.Forms.RadioButton")
                {
                    if (((RadioButton)ctl.Controls[i]).Checked == true)
                    {

                        return (RadioButton)ctl.Controls[i];
                    }
                }

            }
            return null;
        }

        /// <summary>
        /// 设置控件内单选框状态
        /// </summary>
        /// <param name="ctl">容器</param>
        /// <param name="txt">子控件的Text匹配值</param>
        private void setControlRB(Control ctl,string txt)
        {
            //RadioButton rb = new RadioButton();
            for (int i = 0; i < ctl.Controls.Count; i++)
            {
                if (ctl.Controls[i].GetType().FullName == "System.Windows.Forms.RadioButton")
                {
                    if (((RadioButton)ctl.Controls[i]).Text.Equals(txt))
                    {

                        ((RadioButton)ctl.Controls[i]).Checked=true;
                    }
                }

            }
            return;
        }

        //获取Mes部门by工号
        private string getMesDept(string cod_emp)
        {
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();
            string strSQL = "select cod_dept  from fduser u where u.id_usr='" + cod_emp + "'";

            string strResult = DBLink.executeQuery(strSQL, dtTable);
            if (!strResult.Equals("") || dtTable.Rows.Count == 0)
            {
                return "";

            }
            else
            {
                return dtTable.Rows[0][0].ToString();
            }

        }

        //获取Mes部门by工号
        private string getMesDeptAndName(string cod_emp)
        {
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();
            string strSQL = "select d.lin_cod||':'||dsc_cod1  from fduser u " +
                " left outer join (select lin_cod,dsc_cod1 from fdsyscod_d where id_cod = 'CODDPT' and sts_data='R') d" +
                " on u.cod_dept=d.lin_cod" +
                " where u.id_usr='" + cod_emp + "'";

            string strResult = DBLink.executeQuery(strSQL, dtTable);
            if (!strResult.Equals("") || dtTable.Rows.Count == 0)
            {
                return "";

            }
            else
            {
                return dtTable.Rows[0][0].ToString();
            }

        }

        //获取Mes部门by工号
        private string getMesDept(string cod_emp, TextBox txtb)
        {
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();
            string strSQL = "select d.lin_cod||':'||dsc_cod1  from fduser u " +
                " left outer join (select lin_cod,dsc_cod1 from fdsyscod_d where id_cod = 'CODDPT' and sts_data='R') d" +
                " on u.cod_dept=d.lin_cod" +
                " where u.id_usr='" + cod_emp + "'";

            string strResult = DBLink.executeQuery(strSQL, dtTable);
            if (!strResult.Equals("") || dtTable.Rows.Count == 0)
            {
                txtb.Text = "";

            }
            else
            {
                txtb.Text = dtTable.Rows[0][0].ToString();
            }
            dtTable.Dispose();
            dtTable = null;
            return "";

        }

        //获取Mes部门by工号
        private string getMesName(string cod_emp, TextBox txtbox)
        {
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();
            string strSQL = "select nam_usr  from fduser u   where sts_usr <> '99' and  u.id_usr='" + cod_emp + "'";

            string strResult = DBLink.executeQuery(strSQL, dtTable);
            if (!strResult.Equals("") || dtTable.Rows.Count == 0)
            {
                txtbox.Text = "";

            }
            else
            {
                txtbox.Text = dtTable.Rows[0][0].ToString();
            }
            dtTable.Dispose();
            dtTable = null;
            return "";

        }


        //获取ERP部门by工号
        private void getErpDept(string cod_emp, TextBox txtb)
        {
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();
            string strSQL = "select content from tek.codd@tekquery where code_id='CODDPT' and code in(" +
                " select cod_dept from tek.eempb@tekquery where cod_emp='" + cod_emp + "')";

            string strResult = DBLink.executeQuery(strSQL, dtTable);
            if (!strResult.Equals("") || dtTable.Rows.Count == 0)
            {
                txtb.Text = "";
                return;

            }
            else
            {
                txtb.Text = dtTable.Rows[0][0].ToString();
            }
            dtTable.Dispose();
            dtTable = null;
            return;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //dtpOKDate.Text = PPT.Global.getServiceDateAndTime();

        }

        private void txtStartUsr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                getMesDept(txtStartUsr.Text.Trim(), txtStartDept);
            }
        }

        private void txtGetUsr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                getMesDept(txtGetUsr.Text.Trim(), txtGetDept);
            }
        }

        private void txtOKUsr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                getMesDept(txtOKUsr.Text.Trim(), txtOKDept);
            }

        }

        ///判断表是否有记录,有返回记录条数,无返回0
        private int getTmfcngrCount(string id_ngr)
        {
            string sql = "select * from fd_tmfcngr where id_ngr='" + id_ngr + "'";
            DB.DB dblink = new DB.DB();
            DataTable dt = new DataTable();
            string strResult = dblink.executeQuery(sql, dt);

            return dt.Rows.Count;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            #region 处理界面数据
            if (cmbNGItem.Text.Trim().Equals(""))
            {
                lblTips.Text = "不良项目不能为空";
                return;
            }
            string FAST_TYPE = "";//急件类型
            string TN_DEPT = "";//预计的接收部门
            if (getControlRB(gbFastType) == null)
            {
                lblTips.Text = "急件类型未选择";
            }
            else
            {
                FAST_TYPE = getControlRB(gbFastType).Text;
            }

            if (getControlRB(groupBox1) == null)
            {
                lblTips.Text = "接收部门未选择";
            }
            else
            {
                TN_DEPT = getControlRB(groupBox1).Text;
            }

            string ID_NGR = NGR_ID.Text;
            string PROGRESS = txtProgress.Text;
            string STATUSCODE = cmbStatus.Text;
            string NGR_TYPE = cmbNGItem.Text;
            string NUM_PS = cmbNumPs.Text;
            string LOT = cmbLot.Text;
            string SHIFT_PROD = cmbSftProd.Text;
            string NAM_EQ = cmbNamEq.Text;

            //发起人怎么规定 20160721
            string START_USR = strUser;
            string START_DEPT = "";
            if (txtStartDept.Text.IndexOf(":") > 0)
            {
                //START_DEPT = txtStartDept.Text.Substring(0, txtStartDept.Text.IndexOf(":"));
                START_DEPT = txtStartDept.Text;
            }
            string START_DATE = dtpStartDate.Text;//

            //判断是否输入状况描述,并格式加默认title
            string START_NOTE = longWt.Text;
            if (START_NOTE.Trim() == "" || START_NOTE.Trim() == longSartTitle)
            {
                lblTips.Text = "异常状况未填写,无法提交";
                return;
            }
            else
            {
                START_NOTE = START_NOTE.Replace(longSartTitle, "");
                START_NOTE = longSartTitle + START_NOTE;
            }
            //TN_USR
            string TN_USR = txttnUsr.Text;
            string GET_USR = txtGetUsr.Text;
            string GET_DEPT = "";
            if (txtGetDept.Text.IndexOf(":") > 0)
            {
                //GET_DEPT = txtGetDept.Text.Substring(0, txtGetDept.Text.IndexOf(":"));
                GET_DEPT = txtGetDept.Text;
            }
            string GET_DATE = dtpGetDate.Text;//
            string ANY_NOTE = longAny.Text;
            ANY_NOTE = ANY_NOTE.Replace(longGetTitle, "");
            ANY_NOTE = longGetTitle + ANY_NOTE;

            string DO_USR = txtDoUsr.Text;
            string DO_DEPT = "";
            if (txtDoDept.Text.IndexOf(":") > 0)
            {
                //DO_DEPT = txtDoDept.Text.Substring(0, txtDoDept.Text.IndexOf(":"));
                DO_DEPT = txtDoDept.Text;
            }
            string DO_DATE = dtpDoDate.Text;//
            string DO_NOTE = longDo.Text;
            DO_NOTE = DO_NOTE.Replace(longDoTitle, "");
            DO_NOTE = longDoTitle + DO_NOTE;
            string DO_RESULT = cmbDoRst.Text;
            int DO_REMAIN = 0;
            if (!GET_DATE.Equals("") && !DO_DATE.Equals(""))
            {
                TimeSpan ts1 = System.DateTime.Parse(DO_DATE) - System.DateTime.Parse(GET_DATE);
                DO_REMAIN = Convert.ToInt32(ts1.TotalMinutes);
            }

            string OK_USR = txtOKUsr.Text;
            string OK_DEPT = "";
            if (txtOKDept.Text.IndexOf(":") > 0)
            {
                //OK_DEPT = txtOKDept.Text.Substring(0, txtOKDept.Text.IndexOf(":"));
                OK_DEPT = txtOKDept.Text;
            }
            string OK_DATE = dtpOKDate.Text;//;//
            string OK_NOTE = longOK.Text;
            OK_NOTE = OK_NOTE.Replace(longOKTitle, "");
            OK_NOTE = longOKTitle + OK_NOTE;

            //求时间差,单位:分钟
            //START_DATE = "2016/07/18 08:00:00";
            //OK_DATE = "2016/07/18 10:00:01";
            int REMAIN = 0;
            if (!START_DATE.Equals("") && !OK_DATE.Equals(""))
            {
                TimeSpan ts = System.DateTime.Parse(OK_DATE) - System.DateTime.Parse(START_DATE);
                REMAIN = Convert.ToInt32(ts.TotalMinutes);
            }

            #endregion


            List<string> InsertSQL = new List<string>();
            string sql = "";
            if (getTmfcngrCount(ID_NGR) == 0)
            {

                sql = " insert into fd_tmfcngr( " +
                    " ID_NGR,PROGRESS,STATUSCODE,FAST_TYPE,NGR_TYPE, " +
                    " NUM_PS,LOT,SHIFT_PROD,NAM_EQ," +
                    " START_USR,START_DEPT,START_DATE,START_NOTE,TN_USR,TN_DEPT," +
                    " GET_USR,GET_DEPT,GET_DATE,ANY_NOTE," +
                    " DO_USR,DO_DEPT,DO_DATE,DO_NOTE,DO_RESULT,DO_REMAIN," +
                    " OK_USR,OK_DEPT,OK_DATE,OK_NOTE,REMAIN" +
                    " )" +
                    " values ";
                //单独定义values,因为SQL中可能含有&,它被当做变量名
                string strValues = " ('" + ID_NGR + "','" + PROGRESS + "','" + STATUSCODE + "','" + FAST_TYPE + "','" + NGR_TYPE + "'," +
                    " '" + NUM_PS + "','" + LOT + "','" + SHIFT_PROD + "','" + NAM_EQ + "'," +
                    " '" + START_USR + "','" + START_DEPT + "','" + START_DATE + "','" + START_NOTE + "','" + TN_USR + "','" + TN_DEPT + "'," +
                    " '" + GET_USR + "','" + GET_DEPT + "','" + GET_DATE + "','" + ANY_NOTE + "'," +
                    " '" + DO_USR + "','" + DO_DEPT + "','" + DO_DATE + "','" + DO_NOTE + "','" + DO_RESULT + "'," + DO_REMAIN + "'," +
                    " '" + OK_USR + "','" + OK_DEPT + "','" + OK_DATE + "','" + OK_NOTE + "','" + REMAIN + "')";
                strValues = strValues.Replace("&", "'||chr(38)||'");
                InsertSQL.Add(sql + strValues);

                sql = "insert into fh_tmfcngr select * from fd_tmfcngr where ID_NGR='" + ID_NGR + "'";
                InsertSQL.Add(sql);
            }
            else
            {
                sql = " delete from fd_tmfcngr where id_ngr='" + ID_NGR + "'";
                InsertSQL.Add(sql);
                sql = " insert into fd_tmfcngr( " +
                    " ID_NGR,PROGRESS,STATUSCODE,FAST_TYPE,NGR_TYPE, " +
                    " NUM_PS,LOT,SHIFT_PROD,NAM_EQ," +
                    " START_USR,START_DEPT,START_DATE,START_NOTE,TN_USR,TN_DEPT," +
                    " GET_USR,GET_DEPT,GET_DATE,ANY_NOTE," +
                    " DO_USR,DO_DEPT,DO_DATE,DO_NOTE,DO_RESULT,DO_REMAIN," +
                    " OK_USR,OK_DEPT,OK_DATE,OK_NOTE,REMAIN" +
                    " )" +
                    " values ";
                //单独定义values,因为SQL中可能含有&,它被当做变量名
                string strValues = " ('" + ID_NGR + "','" + PROGRESS + "','" + STATUSCODE + "','" + FAST_TYPE + "','" + NGR_TYPE + "'," +
                    " '" + NUM_PS + "','" + LOT + "','" + SHIFT_PROD + "','" + NAM_EQ + "'," +
                    " '" + START_USR + "','" + START_DEPT + "','" + START_DATE + "','" + START_NOTE + "','" + TN_USR + "','" + TN_DEPT + "'," +
                    " '" + GET_USR + "','" + GET_DEPT + "','" + GET_DATE + "','" + ANY_NOTE + "'," +
                    " '" + DO_USR + "','" + DO_DEPT + "','" + DO_DATE + "','" + DO_NOTE + "','" + DO_RESULT + "','" + DO_REMAIN + "'," +
                    " '" + OK_USR + "','" + OK_DEPT + "','" + OK_DATE + "','" + OK_NOTE + "','" + REMAIN + "')";
                strValues = strValues.Replace("&", "'||chr(38)||'");
                InsertSQL.Add(sql + strValues);

                sql = "insert into fh_tmfcngr select * from fd_tmfcngr where ID_NGR='" + ID_NGR + "'";
                InsertSQL.Add(sql);
            }

            DB.DB dblink = new DB.DB();
            string strResult = dblink.executeTrx(InsertSQL.ToArray());

            if (strResult == "")
            {
                MessageBox.Show("写入成功!");
                btnSubmit.Enabled = false;
                groupBox1.Enabled = false;
            }

        }

        private void txtStartUsr_TextChanged(object sender, EventArgs e)
        {
            getMesDept(txtStartUsr.Text.Trim(), txtStartDept);
            getMesName(txtStartUsr.Text, txtStartName);
        }

        private void txtGetUsr_TextChanged(object sender, EventArgs e)
        {
            getMesDept(txtGetUsr.Text.Trim(), txtGetDept);
            getMesName(txtGetUsr.Text, txtGetName);
        }

        private void txtDoUsr_TextChanged(object sender, EventArgs e)
        {
            getMesDept(txtDoUsr.Text.Trim(), txtDoDept);
            getMesName(txtDoUsr.Text, txtDoName);
        }

        private void txtOKUsr_TextChanged(object sender, EventArgs e)
        {
            getMesDept(txtOKUsr.Text.Trim(), txtOKDept);
            getMesName(txtOKUsr.Text, txtOKName);
        }

        

        private void txtStartDept_TextChanged(object sender, EventArgs e)
        {
            if (txtStartDept.Text.Equals(""))
            {
                dtpStartDate.Text = "";
                txtStartName.Text = "";
            }
            else
            {
                dtpStartDate.Text = PPT.Global.getServiceDateAndTime();
            }
        }

        private void txtGetDept_TextChanged(object sender, EventArgs e)
        {
            if (txtGetDept.Text.Equals(""))
            {
                dtpGetDate.Text = "";
                txtGetName.Text = "";
            }
            else
            {
                dtpGetDate.Text = PPT.Global.getServiceDateAndTime();                
            }
        }

        private void txtDoDept_TextChanged(object sender, EventArgs e)
        {
            if (txtDoDept.Text.Equals(""))
            {
                dtpDoDate.Text = "";
                txtDoName.Text = "";
            }
            else
            {
                dtpDoDate.Text = PPT.Global.getServiceDateAndTime();
            }
        }

        private void cmbDoRst_TextChanged(object sender, EventArgs e)
        {
            if (txtDoDept.Text.Equals(""))
            {
                dtpDoDate.Text = "";
            }
            else
            {
                dtpDoDate.Text = PPT.Global.getServiceDateAndTime();
            }
        }

        private void txtOKDept_TextChanged(object sender, EventArgs e)
        {
            if (txtOKDept.Text.Equals(""))
            {
                dtpOKDate.Text = "";
                txtOKName.Text = "";
            }
            else
            {
                //dtpOKDate.Text = PPT.Global.getServiceDateAndTime();
            }
        }

        private void cmbStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtOKDept.Text.Equals(""))
            {
                dtpOKDate.Text = "";
            }
            else
            {
                dtpOKDate.Text = PPT.Global.getServiceDateAndTime();                
            }

            //改变结案状态时,填充结案人为登录人
            //新建时不填充,未结案时不填充
            if (txtProgress.Text == "修改" && cmbStatus.Text != "未结案")
            {
                txtOKUsr.Text = strUser;
            }
            else
            {
                txtOKUsr.Text = "";
            }
        }
        //获取不良名称 By baggio 20161208
        private void GetcmbNGItem()
        {
            DataTable dtTable = new DataTable();
            DB.DB DBLink = new DB.DB();

            string strSQL = "select DSC_COD1||DSC_COD2 defect from fdsyscod_d where id_cod='DEFECT' and sts_data='R' ORDER BY DSC_COD1,DSC_COD2  ";
            string strResult = DBLink.executeQuery(strSQL, dtTable);
            //if (!strResult.Equals(""))
            {
                for (int i = 0; i < dtTable.Rows.Count - 1; i++)
                    cmbNGItem.Items.Add(dtTable.Rows[i][0].ToString());
            }
            

            dtTable.Dispose();
            dtTable = null;

        }      

        


    }
}
